<template>
  <div class="page-bg">
    <listTable
      ref="tableRef"
      v-model="tableData"
      @getList="getList"
      :isCheck="false"
    >
      <template #action>
        <el-button type="primary" @click="openEditFn()">新增管理员</el-button>
      </template>
      <el-table-column prop="id" label="ID" width="50"></el-table-column>
      <el-table-column prop="name" label="昵称"></el-table-column>
      <el-table-column prop="user" label="账号"></el-table-column>
      <el-table-column prop="notes" label="备注"></el-table-column>
      <el-table-column label="角色" width="150">
        <template #default="scope">
          <div v-for="(v, i) in tableData.types" :key="i">
            <span v-if="scope.row.role_id == v.id">{{ v.name }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column align="center" width="80" label="状态">
        <template #default="scope">
          <el-tag v-if="scope.row.a_show == '1'" type="success">启用</el-tag>
          <el-tag v-else type="info">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="登录时间" prop="login_time" width="180" />
      <el-table-column label="创建时间" prop="create_time" width="180" />
      <el-table-column fixed="right" label="操作栏" width="170" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            size="small"
            @click="openEditFn(scope.row)"
            >编辑</el-button
          >
          <el-popconfirm
            title="确定重置密码？"
            @confirm="resetPass(scope.row.id)"
          >
            <template #reference>
              <el-button link type="primary" size="small">重置密码</el-button>
            </template>
          </el-popconfirm>
          <el-popconfirm title="确定删除？" @confirm="delFn(scope.row.id)">
            <template #reference>
              <el-button link type="primary" size="small">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </listTable>
    <el-dialog
      v-model="editData.show"
      :title="`${editData.form?.id ? '编辑' : '新增'}管理员`"
      width="500px"
    >
      <el-form :model="editData.form" label-width="100px">
        <el-form-item label="角色：">
          <el-select v-model="editData.form.role_id" placeholder="请选择角色">
            <el-option
              v-for="item in tableData.types"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="昵称：">
          <el-input
            v-model="editData.form.name"
            autocomplete="off"
            clearable
            placeholder="请输入"
          />
        </el-form-item>
        <el-form-item label="账号：">
          <el-input
            v-model="editData.form.user"
            autocomplete="off"
            clearable
            placeholder="请输入"
          />
        </el-form-item>
        <el-form-item label="备注：">
          <el-input
            v-model="editData.form.notes"
            autocomplete="off"
            clearable
            placeholder="请输入"
          />
        </el-form-item>
        <el-form-item label="是否启用：">
          <el-radio v-model="editData.form.a_show" label="1">是</el-radio>
          <el-radio v-model="editData.form.a_show" label="0">否</el-radio>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="editData.show = false">取消</el-button>
          <el-button type="primary" @click="subForm">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { listTable, typeColumn } from "@/components/listPage";
import { tableDataType, tableDataQuery } from "@/common/interface";
import { post } from "@/common/http";
import dayjs from "dayjs";
import { ElMessageBox, ElMessage } from "element-plus";
const tableRef = ref();

const router = useRouter();

class listItem {
  id?: number;
  user: string;
  name: string;
  phone: string;
  email: string;
  notes: string;
  a_show: string;
  role_id: number;
  login_time: number;
  create_time: number;
  constructor() {
    this.user = "";
    this.name = "";
    this.phone = "";
    this.email = "";
    this.notes = "";
    this.a_show = "1";
    this.role_id = 179;
    this.login_time = 0;
    this.create_time = 0;
  }
}

//表格组件数据
const tableData = reactive<tableDataType<listItem>>(new tableDataType());

const typeID = ref(""); //筛选分类
const query = ref<tableDataQuery>(new tableDataQuery());

//筛选分类
function changeSelectFn() {
  getList(query.value);
}

//获取列表
function getList(data: tableDataQuery): void {
  query.value = data;
  post("api/Admin/list_data_a", {
    page: data.page,
    limit: data.limit,
    type: typeID.value,
    keyWord: data.searchVal,
    prop: "id", //需要排序的字段
    sort: "desc", //desc asc
  }).then((res) => {
    tableData.types = res.data.type;
    tableData.types.push({
      id:999999999,
      name:'超级管理员'
    })
    tableData.lists = res.data.list.map((v: listItem) => {
      return {
        ...v,
        login_time: dayjs(v.login_time * 1000).format("YYYY-MM-DD HH:mm:ss"),
        create_time: dayjs(v.create_time * 1000).format("YYYY-MM-DD HH:mm:ss"),
      };
    });
    tableData.total = res.total ?? 0;
    tableData.loading = false;
  });
}

//删除
function delFn(id: number) {
  if (id === 9) {
    return ElMessageBox.alert("超级管理员，不能删除", "系统提示");
  }
  post("api/Admin/del_a", { id }).then(() => tableRef.value.reload());
}

//重置密码
async function resetPass(id: number) {
  await post("api/Admin/resetPass", { id });
  return ElMessage.success("重置成功");
}

class editDataType {
  show: boolean;
  form: listItem;
  constructor() {
    this.show = false;
    this.form = new listItem();
  }
}

const editData = reactive<editDataType>(new editDataType());

function openEditFn(data?: listItem) {
  if (data) {
    editData.form = JSON.parse(JSON.stringify(data));
  } else {
    editData.form = new listItem();
    editData.form.role_id = tableData.types[0].id;
  }
  editData.show = true;
}

const subForm = () => {
  let form: listItem = editData.form;
  if (!form.role_id) return ElMessage.warning("请选择角色");
  if (!form.name) return ElMessage.warning("请输入昵称");
  if (!form.user) return ElMessage.warning("请输入账号");
  post("api/Admin/saveData", form).then((res) => {
    tableRef.value.reload();
    editData.show = false;
  });
};
</script>

<style lang="scss" scoped>
.el-form {
  padding-right: 30px;
}
.search {
  .b1 {
    margin-left: 10px;
  }
}
</style>
